package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.R;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.view.ui.activity.packers.MapFindWorkActivity;
import com.sgb.link.view.adapter.PickAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

public class MapFindWorksFilterPopWindow extends BasePopWindow {

    private MapFindWorkActivity activity;
    private FilterResultClick filterResultClick;

    private TextView okView;
    private TextView reSetView;
    private MaxRecyclerView rvMajor;
    private MaxRecyclerView rvBusiness;
    private MaxRecyclerView rvChild;

    //工程专项 adapter
    private PickAdapter majorAdapter;
    //专业分类 adapter
    private PickAdapter businessAdapter;
    //分项工程 adapter
    private PickAdapter childAdapter;

    private PickResultEntity pickResultMajor;
    private PickResultEntity pickResultBusiness;
    private PickResultEntity pickResultChild;

    public MapFindWorksFilterPopWindow(Context context) {
        super(context, DIR_RIGHT);
        activity = (MapFindWorkActivity) context;
    }

    @Override
    protected int popLayout() {
        return R.layout.popwin_packers_filter;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        okView = holder.getPop().findViewById(R.id.ok_view);
        reSetView = holder.getPop().findViewById(R.id.reset_view);

        //工程专项rv
        rvMajor = holder.getPop().findViewById(R.id.rv_major);
        //专业分类rv
        rvBusiness = holder.getPop().findViewById(R.id.rv_business);
        //分项工程rv
        rvChild = holder.getPop().findViewById(R.id.rv_child);

        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                filterResultClick.result(pickResultMajor, pickResultBusiness, pickResultChild);
                dissMiss();
            }
        });

        reSetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                majorAdapter.resetCheckDatas();
                businessAdapter.resetCheckDatas();
                childAdapter.resetCheckDatas();

                majorAdapter.setDefItem(-1);
                businessAdapter.setDefItem(-1);
                childAdapter.setDefItem(-1);
            }
        });

        majorAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        majorAdapter.setItemSelect(true);
        majorAdapter.setDefItem(-1);
        rvMajor.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvMajor.setAdapter(majorAdapter);
        majorAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                pickResultMajor.setNo(pickSelectEntity.getNo());
                pickResultMajor.setName(pickSelectEntity.getProjectscale());
                pickResultMajor.setPosition(position);
                pickResultBusiness.clear();
                pickResultChild.clear();
                activity.viewModel.getThrCategory(pickSelectEntity.getNo(), true);

            }
        });

        businessAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        businessAdapter.setItemSelect(true);
        businessAdapter.setDefItem(-1);
        rvBusiness.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvBusiness.setAdapter(businessAdapter);
        businessAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                pickResultBusiness.setNo(pickSelectEntity.getNo());
                pickResultBusiness.setName(pickSelectEntity.getProjectscale());
                pickResultBusiness.setPosition(position);
                pickResultChild.clear();
                activity.viewModel.getFourCategory(pickSelectEntity.getNo());

            }
        });

        childAdapter = new PickAdapter(mContext, null, R.layout.item_pick_filter_child);
        childAdapter.setItemSelect(true);
        childAdapter.setDefItem(-1);
        rvChild.setLayoutManager(new GridLayoutManager(mContext, 3));
        rvChild.setAdapter(childAdapter);
        childAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                pickResultChild.setNo(pickSelectEntity.getNo());
                pickResultChild.setName(pickSelectEntity.getProjectscale());
                pickResultChild.setPosition(position);
            }
        });
    }

    public void setMajorData(List<PickSelectEntity> data, PickResultEntity entity) {
        majorAdapter.setDatas(data);
        majorAdapter.setDefItem(entity.getPosition());
        if (pickResultMajor == null){
            pickResultMajor = new PickResultEntity();
        }
        pickResultMajor.replace(entity);
    }

    public void setBusinessData(List<PickSelectEntity> data, PickResultEntity entity, boolean isRight) {
        businessAdapter.setDatas(data);
        businessAdapter.setDefItem(entity.getPosition());
        if (pickResultBusiness == null) {
            pickResultBusiness = new PickResultEntity();
        }
        if (!isRight) {
            pickResultBusiness.replace(entity);
        }
    }

    public void setChildData(List<PickSelectEntity> data, PickResultEntity entity, boolean isRight) {
        childAdapter.setDatas(data);
        childAdapter.setDefItem(entity.getPosition());
        if (pickResultChild == null) {
            pickResultChild = new PickResultEntity();
        }
        if (!isRight) {
            pickResultChild.replace(entity);
        }
    }

    public interface FilterResultClick {

        void result(PickResultEntity pickResultM, PickResultEntity pickResultB, PickResultEntity pickResultC);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }
}
